package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IUserNoticeService;
import com.mdd.admin.validate.UserNoticeCreateValidate;
import com.mdd.admin.validate.UserNoticeUpdateValidate;
import com.mdd.admin.validate.UserNoticeSearchValidate;
import com.mdd.admin.vo.UserNoticeListedVo;
import com.mdd.admin.vo.UserNoticeDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.UserNotice;
import com.mdd.common.mapper.UserNoticeMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 用户通知设置实现类
 * @author DEREK
 */
@Service
public class UserNoticeServiceImpl implements IUserNoticeService {
        
    @Resource
    UserNoticeMapper userNoticeMapper;

    /**
     * 用户通知设置列表
     *
     * @author DEREK
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<UserNoticeListedVo>
     */
    @Override
    public PageResult<UserNoticeListedVo> list(PageValidate pageValidate, UserNoticeSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<UserNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        userNoticeMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:userId@user_id:int",
            "=:noticeOne@notice_one:int",
            "=:noticeTwo@notice_two:int",
            "=:noticeThree@notice_three:int",
            "=:noticeFour@notice_four:int",
            "=:noticeFive@notice_five:int",
            "=:noticeSix@notice_six:int",
            "=:noticeSeven@notice_seven:int",
            "=:noticeEight@notice_eight:int",
            "=:noticeNine@notice_nine:int",
        });

        IPage<UserNotice> iPage = userNoticeMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<UserNoticeListedVo> list = new LinkedList<>();
        for(UserNotice item : iPage.getRecords()) {
            UserNoticeListedVo vo = new UserNoticeListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 用户通知设置详情
     *
     * @author DEREK
     * @param id 主键参数
     * @return UserNotice
     */
    @Override
    public UserNoticeDetailVo detail(Integer id) {
        UserNotice model = userNoticeMapper.selectOne(
                new QueryWrapper<UserNotice>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        UserNoticeDetailVo vo = new UserNoticeDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 用户通知设置新增
     *
     * @author DEREK
     * @param createValidate 参数
     */
    @Override
    public void add(UserNoticeCreateValidate createValidate) {
        UserNotice model = new UserNotice();
        model.setUserId(createValidate.getUserId());
        model.setNoticeOne(createValidate.getNoticeOne());
        model.setNoticeTwo(createValidate.getNoticeTwo());
        model.setNoticeThree(createValidate.getNoticeThree());
        model.setNoticeFour(createValidate.getNoticeFour());
        model.setNoticeFive(createValidate.getNoticeFive());
        model.setNoticeSix(createValidate.getNoticeSix());
        model.setNoticeSeven(createValidate.getNoticeSeven());
        model.setNoticeEight(createValidate.getNoticeEight());
        model.setNoticeNine(createValidate.getNoticeNine());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        userNoticeMapper.insert(model);
    }

    /**
     * 用户通知设置编辑
     *
     * @author DEREK
     * @param updateValidate 参数
     */
    @Override
    public void edit(UserNoticeUpdateValidate updateValidate) {
        UserNotice model = userNoticeMapper.selectOne(
                new QueryWrapper<UserNotice>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUserId(updateValidate.getUserId());
        model.setNoticeOne(updateValidate.getNoticeOne());
        model.setNoticeTwo(updateValidate.getNoticeTwo());
        model.setNoticeThree(updateValidate.getNoticeThree());
        model.setNoticeFour(updateValidate.getNoticeFour());
        model.setNoticeFive(updateValidate.getNoticeFive());
        model.setNoticeSix(updateValidate.getNoticeSix());
        model.setNoticeSeven(updateValidate.getNoticeSeven());
        model.setNoticeEight(updateValidate.getNoticeEight());
        model.setNoticeNine(updateValidate.getNoticeNine());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        userNoticeMapper.updateById(model);
    }

    /**
     * 用户通知设置删除
     *
     * @author DEREK
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        UserNotice model = userNoticeMapper.selectOne(
                new QueryWrapper<UserNotice>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        userNoticeMapper.delete(new QueryWrapper<UserNotice>().eq("id", id));
    }

}
